package cn.edu.scau.cmil.shishuai.comprehensive.address.view;

import java.io.File;

import javax.swing.JOptionPane;

import cn.edu.scau.cmil.shishuai.comprehensive.address.MainApp;
import cn.edu.scau.cmil.shishuai.comprehensive.address.model.Person;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;

public class LoginController {
	
	public static Person userperson = new Person();
	
	@FXML
	private Button btn_sign;
	
	@FXML
	private Button btn_login;
	
	@FXML
	private TextField text_user;
	
	@FXML
	private TextField text_password;
	
	@FXML
	private RadioButton rtn_student;
	
	@FXML 
	private RadioButton rtn_teacher;
	
	public MainApp mainApp = new MainApp();
	
	final ToggleGroup stgroup = new ToggleGroup();
	
	@FXML
	protected void onRadioButtonClick(ActionEvent event) {

	}
	@FXML
	protected void onLoginButtonClick(ActionEvent event) {
		
		File personFile = mainApp.getPersonFilePath();
        if (personFile != null) {
        mainApp.loadPersonDataFromFile(personFile);
        
        
		String nuser = text_user.getText();
		if(rtn_teacher.isSelected()) {
		if(mainApp.findperson(nuser) != null ) {
		if(mainApp.findperson(nuser).getPassword().equals(text_password.getText()) && mainApp.findperson(nuser).getLabel().equals("T") ) {
		
			userperson = mainApp.findperson(nuser);
			mainApp.initRootLayout();
		
		exit();
		}
		else {
	    	JOptionPane.showMessageDialog(null, "请检查密码", "错误", JOptionPane.ERROR_MESSAGE);

		}
		}
		else {
	    	JOptionPane.showMessageDialog(null, "请检查用户名", "错误", JOptionPane.ERROR_MESSAGE);

		}
		}else {
			if(mainApp.findperson(nuser) != null ) {
				if(mainApp.findperson(nuser).getPassword().equals(text_password.getText()) && mainApp.findperson(nuser).getLabel().equals("S") ) {
				
					userperson = mainApp.findperson(nuser);

					mainApp.Studentview();
				
				exit();
				}
				else {
			    	JOptionPane.showMessageDialog(null, "请检查密码", "错误", JOptionPane.ERROR_MESSAGE);

				}
				}
				else {
			    	JOptionPane.showMessageDialog(null, "请检查用户名", "错误", JOptionPane.ERROR_MESSAGE);

				}
				}
			
		}
        }
		

	
	public void exit() {
		Stage stage = (Stage)btn_sign.getScene().getWindow();
	    stage.close();
	}
	
	@FXML
	protected void onSignButtonClick(ActionEvent event) {
		mainApp.showStudentSignview();
    
    }
	@FXML
	private void initialize() {
		
		File personFile = mainApp.getPersonFilePath();
        if (personFile != null) {
        mainApp.loadPersonDataFromFile(personFile);
        } 

		
		rtn_student.setToggleGroup(stgroup);
		rtn_teacher.setToggleGroup(stgroup);
		text_user.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if(newValue.matches("[0-9]{0,20}")!=true) text_user.setText(oldValue);;
				
			}
		});
		
	}

}
